<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/28
 * Time: 9:31
 */
$http = new swoole_http_server('0.0.0.0', 8811);

$http->set([
    'worker_num' => 6,
    'enable_static_handler' => true,
    'document_root' => "/data/wwwroot/swoole/thinkphp/public/static",
]);
//此事件在Worker进程/Task进程启动时发生，初始化引入thinkphp必需文件
$http->on('WorkerStart', function(swoole_server $server, $worker_id){
    // 定义应用目录
    define('APP_PATH', __DIR__ . '/../application/');
    // 1. 加载基础文件
    require __DIR__ . '/../thinkphp/base.php';
    //require __DIR__ . '/../thinkphp/start.php';
});

$http->on('request', function($request, $reponse)use($http){
    $_SERVER = [];
    if(isset($request->server)){
        foreach($request->server as $k=>$v){
            $_SERVER[strtoupper($k)] = $v;
        }
    }

    if(isset($request->header)){
        foreach($request->header as $k=>$v){
            $_SERVER[strtoupper($k)] = $v;
        }
    }
    $_GET = [];
    if(isset($request->get)){
        foreach($request->get as $k=>$v){
            $_GET[$k] = $v;
        }
    }
    $_POST = [];
    if(isset($request->post)){
        foreach($request->post as $k=>$v){
            $_POST[$k] = $v;
        }
    }

    ob_start();
    // 执行应用并响应
    try{
        think\Container::get('app', [APP_PATH])
            ->run()
            ->send();
    }catch (\Exception $e){
        //todo
    }
    //echo '---action' . request()->action() . PHP_EOL;
    $content = ob_get_contents();
    ob_end_clean();

    $reponse->end($content);

    //$http->close();
});

$http->start();